package com.android.fragmentdemo.defineviews
import android.content.Context
import android.graphics.BitmapFactory
import android.graphics.Camera
import android.graphics.Canvas
import android.graphics.Matrix
import android.graphics.Paint
import android.util.AttributeSet
import android.util.Log
import android.view.View
import com.android.fragmentdemo.R

class FoldImageView @JvmOverloads constructor(
    context: Context,
    attributeSet: AttributeSet? = null
) : View(context, attributeSet) {

    private val mShadowPaint by lazy {
        Paint().apply {
            isAntiAlias = true
        }
    }

    private val mMatrix by lazy {
        Matrix()
    }

    private val mCamera by lazy {
        Camera().apply {
            rotateY(-45.0F)
            setLocation(0F, 0F, -16 * resources.displayMetrics.density)
        }
    }

    private val bmp = BitmapFactory.decodeResource(context.resources, R.drawable.p10)

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        setMeasuredDimension(bmp.width, bmp.height)
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        Log.d(TAG,"measuredWidth:${measuredWidth};measuredHeight:${measuredHeight}")
        canvas.apply {
//            //绘制上半部分
//            save()
//            //将camera移动到要画的图片中心上方
//            //translate(bmp.width / 2F, bmp.height / 2F)
//            //canvas旋转-20度，为了实现斜切效果
//            rotate(0F)
//            //截取部分区域，大小只要能保证足够图片上半部分显示即可
//            clipRect(bmp.width / 3, 0, bmp.width, bmp.height)
//            //canvas再旋转回来
//            rotate(0F)
//            //将虚拟摄像头移动回原点
//            //translate(-bmp.width / 2F, -bmp.height / 2F)
//            //开始画图
//            drawBitmap(bmp, 0F, 0F, mShadowPaint)
//            restore()
//
//            //绘制下半部分
//            save()
//            //将camera移动到要画的图片中心上方
//            //translate(bmp.width / 2F, bmp.height / 2F)
//            rotate(0F)
//            //实现折叠效果
//            mCamera.applyToCanvas(this)
//            //截取部分区域，大小只要能保证足够图片下半部分显示即可
//            clipRect(0, 0, bmp.width / 3, bmp.height)
//            rotate(0F)
//            //将虚拟摄像头移动回原点
//            //translate(-bmp.width / 2F, -bmp.height / 2F)
//            drawBitmap(bmp, 0F, 0F, mShadowPaint)
//            restore()

//            save()
//            clipRect(0F, 0F, bmp.getWidth().toFloat() / 2, bmp.getHeight().toFloat())
//            val srcPoints = floatArrayOf(0F, 0F, bmp.getWidth().toFloat() * 2 / 4, bmp.getHeight().toFloat() * 2 / 4)
//            val dstPoints = floatArrayOf(0F, 0F, bmp.getWidth().toFloat(), bmp.getHeight().toFloat())
//            mMatrix.setPolyToPoly(srcPoints, 0, dstPoints, 0, 2)
//            drawBitmap(bmp, mMatrix, mShadowPaint)
//            restore()
//
//            save()
//            //mCamera.applyToCanvas(this)
//            clipRect(bmp.getWidth().toFloat() / 2, 0F, bmp.getWidth().toFloat() , bmp.getHeight().toFloat())
//            val srcPoints1 = floatArrayOf(bmp.getWidth().toFloat() / 2, 0F, bmp.getWidth().toFloat(), bmp.getHeight().toFloat())
//            val dstPoints1 = floatArrayOf(bmp.getWidth().toFloat() / 2, 0F, bmp.getWidth().toFloat(), bmp.getHeight().toFloat())
//            mMatrix.setPolyToPoly(srcPoints1, 0, dstPoints1, 0, 2)
//            drawBitmap(bmp, mMatrix, mShadowPaint)
//            restore()

            val srcPoints1 = floatArrayOf(
                0F,
                measuredHeight.toFloat(),

                0F,
                0F,

                measuredWidth.toFloat(),
                0F,

                measuredWidth.toFloat(),
                measuredHeight.toFloat()
            )

            val dstPoints1 = floatArrayOf(
                0F + 150F,
                measuredHeight.toFloat() + 150F,

                0F,
                0F,

                measuredWidth.toFloat(),
                0F,

                measuredWidth.toFloat(),
                measuredHeight.toFloat()
            )

            mMatrix.setPolyToPoly(srcPoints1, 0, dstPoints1, 0, 4)

            drawBitmap(bmp, mMatrix, mShadowPaint)
        }
    }

    companion object {
        const val TAG = "FoldImageView"
    }
}